﻿using System;
using System.Collections.Generic;
using System.Web;
using NewLife.Web;
using System.Collections.Specialized;
 

namespace BingZi.WebControls
{
    /// <summary>
    ///前面分页类
    /// </summary>
    public class Pager 
    {
        private Int32 PageIndex = 1;
        private Int32 PageSize = 10;
        private Int32 RecordCount = 0;
        private Boolean IsDnnPager = true;
        private Boolean IsParameter = false;
 


        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="__PageIndex">页码</param>
        /// <param name="__ModuleId"></param>
        /// <param name="__PageSize"></param>
        /// <param name="__RecordCount"></param>
        public Pager(Int32 __PageIndex, Int32 __PageSize, Int32 __RecordCount)
            : this(__PageIndex, __PageSize, __RecordCount, false)
        {}
 

        public Pager(Int32 __PageIndex, Int32 __PageSize, Int32 __RecordCount, Boolean __IsParameter)
        {
            PageIndex = __PageIndex;
            PageSize = __PageSize;
       
            RecordCount = __RecordCount;
            IsParameter = __IsParameter;
 
        }
 

        /// <summary>
        /// 创建HTML
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="RecordCount"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public  string CreateHtml()
        {
            System.Text.StringBuilder html = new System.Text.StringBuilder();
           

            if (RecordCount == 0)
            {
                return html.ToString();
            }

            int pageCount = RecordCount / PageSize;


            if (RecordCount % PageSize > 0)
            {
                pageCount += 1;
            }
            string total = string.Empty;
            string left = string.Empty;
            string right = string.Empty;
            string center = string.Empty;
          

            //显示首页 上一页
            if (PageIndex != 1)
            {
                left += "<a class=\"button\" href=\"" + CreateUrl(1) + "\">首页</a>";
                left += "<a class=\"button\" href=\"" + CreateUrl(PageIndex - 1) + "\">尾页</a>";
            }
            else
            {
                left += "<span class=\"disabled\">首页</span>";
                left += "<span class=\"disabled\">尾页</span>";
            }

            //显示尾页 下一页
            if (PageIndex < pageCount && pageCount > 1)
            {
                right += "<a class=\"button\" href=\"" + CreateUrl(PageIndex + 1) + "\">下一页</a>";
                right += "<a class=\"button\" href=\"" + CreateUrl(pageCount) + "\">上一页</a>";
            }
            else
            {
                right += "<span class=\"disabled\">下一页</span>";
                right += "<span class=\"disabled\">上一页</span>";
            }

            int min = 1;	//要显示的页面数最小值
            int max = pageCount;   	//要显示的页面数最大值

            if (pageCount > 5)
            {
                if (PageIndex > 2 && PageIndex < (pageCount - 1))
                {
                    min = PageIndex - 2;
                    max = PageIndex + 2;
                }
                else if (PageIndex <= 2)
                {
                    min = 1;
                    max = 5;
                }

                else if (PageIndex >= (pageCount - 1))
                {
                    min = pageCount - 4;
                    max = pageCount;
                }
            }


            //循环显示数字
            for (int i = min; i <= max; i++)
            {
                if (PageIndex == i)	//如果是当前页，用粗体和红色显示
                {
                    center += "<span class=\"disabled\">[" + i + "]</span>";
                }
                else
                {
                    center += "<a class=\"button\" href=\"" + CreateUrl(i) +"\">" + i + "</a>";
                }
            }

            total = string.Format("第{0}页/共{1}页", PageIndex, pageCount);

            html.Append("<div class=\"pager\">");
            html.Append(total + left + center + right);
            html.Append( "</div>");



            return html.ToString();

        }



        /// <summary>
        /// 创建连接
        /// </summary>
        /// <param name="_PageIndex"></param>
        /// <returns></returns>
        private String CreateUrl(Int32 _PageIndex)
        {
           
                String indexString = "pageindex";
                String QueryString = String.Empty;

                if (IsParameter)
                {
                    List<String> Querys = new List<String>();

                    NameValueCollection QueryStrings = HttpContext.Current.Request.QueryString;

                    foreach (String Qkey in QueryStrings.AllKeys)
                    {
                        if (indexString != Qkey.ToLower())
                        {
                            QueryString += String.Format("{0}{1}={2}", QueryString == String.Empty ? "?" : "&", Qkey, QueryStrings[Qkey]);
                        }
                    }
                }

                QueryString += String.Format("{0}{1}={2}", QueryString == String.Empty ? "?" : "&", indexString, _PageIndex);
                return String.Format("{0}{1}", Common.GetScriptName, QueryString);


               
 
        }
    }
}